import java.util.Arrays;

public class TextHeap {

    public int[] arr;
    public int size;

    public TextHeap(int[] arr) {
        this.arr = arr;
        size = arr.length;
    }

    public void createheap() {
        for (int i = (size - 1 - 1) / 2; i >= 0; i--) {
            shiftDown(i, size);
        }
    }

    private void shiftDown(int parent, int len) {
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if (arr[parent] < arr[child]) {
                Swap(parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    private void Swap(int x, int y) {
        int tmp = this.arr[x];
        this.arr[x] = this.arr[y];
        this.arr[y] = tmp;
    }

    private void shiftUp(int child) {
        int parent=(child-1)/2;
        while (child>0){
            if(arr[parent]<arr[child]){
                Swap(parent,child);
                child=parent;
                parent=(child-1)/2;
            }else{
                break;
            }
        }
    }

    public void offer(int val) {
        if (isfull()) {
            arr = Arrays.copyOf(arr, 2 * arr.length);
        }
        arr[size++] = val;
        shiftUp(size-1);
    }

    private boolean isfull() {
        return arr.length == size;
    }
}
